package com.lv.hechuang.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.lv.hechuang.Constants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static String TAG = "WXEntryActivity";

    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, Constants.WX_APPID, false);

        try {
            Intent intent = getIntent();
            boolean b = api.handleIntent(intent, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:

                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:

                break;
            default:
                break;
        }
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        int result = 0;

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                String code = ((SendAuth.Resp) resp).code;
                login(code);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
                finish();
                break;
            default:
                finish();
                break;
        }

    }

    private void login(String code) {
//        Intent intent = new Intent(WXEntryActivity.this, LoginActivity.class);
//        intent.putExtra("code", code);
////        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //关键的一句，将新的activity置为栈顶
//        setResult(111, intent);
//        startActivity(intent);
        Constants.WECHAT_CODE = code;
        finish();
    }

//    private void login(String code) {
//        Log.e(TAG, code);
////        Logger.i(TAG, code + "//121212");
//        XRetrofitUtils retrofitUtils = new XRetrofitUtils.Builder()
//                .setUrl(CommonBaseUrl.wechatLogin)
//                .setParams("code", code)
//                .build();
//        retrofitUtils.AsynPost(new XRetrofitUtils.OnResultListener() {
//            @Override
//            public void onNetWorkError() {
//                Logger.e("WXEntryActivity", "onNetWorkError");
//            }
//
//            @Override
//            public void onSuccess(String data) {
//                Gson gson = new Gson();
//                LoginBean loginBean = gson.fromJson(data, LoginBean.class);
//                if (loginBean != null) {
//                    setResult(ResultCodeInfo.WECHAT_RESULT);
//                    Intent intent = new Intent(WXEntryActivity.this, BindPhoneActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("login_info", loginBean);
//                    intent.putExtras(bundle);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //关键的一句，将新的activity置为栈顶
//                    startActivityForResult(intent, ResultCodeInfo.WECHAT_RESULT);
//                    finish();
//                } else {
//                    ToastUtils.showMessage(R.string.wechat_login_err);
//                    finish();
//                }
//            }
//
//            @Override
//            public void onNoData(String data) {
//                Logger.e("WXEntryActivity", "onNoData");
//            }
//
//            @Override
//            public void onServerError() {
//                Logger.e("WXEntryActivity", "onServerError");
//            }
//        });
//
////        TUIKit.login(Constants.USER_ID,
////                Constants.USER_SIG, new IUIKitCallBack() {
////                    @Override
////                    public void onSuccess(Object data) {
////
////                        Intent intent = new Intent(WXEntryActivity.this, MainActivity.class);
////                        startActivity(intent);
////                        getHwToken();
////                        finish();
////                    }
////
////                    @Override
////                    public void onError(String module, int errCode, String errMsg) {
////
////                    }
////                });
//    }

}